<?php
class Menu
{
	private $oMenu = NULL;
	
	// Building the menu...
	private function buildMenu()
	{
		global $oADODB;
		//$oADODB->debug = true;
		
		// get all the links
		$oLinks = new daoLinks($oADODB);
		// user filter
		$oLinks->addFilter("iUseID", "=", $_SESSION['iUseID']);
		// social filter
		//$oLinks->addFilter("iSocialID", "=", "null");
		//$oLinks->addFilter("bRoot", "=", 1);
		$oLinks->addOrder(null, 'iLnkID', 'ASC');
		$rsLinks = $oLinks->getAll();
		// keep it in session		
		$_SESSION['aLinks'] = $rsLinks->getRows();
		
		// trae los modulos de relacion
		$oRelLinks = new daoRelLinks($oADODB);
		// order by parent
		$oRelLinks->addOrder(null, 'iRelParentID');
		$rsRelLinks = $oRelLinks->getAll();
		$_SESSION['aRelLinks'] = $rsRelLinks->getRows();

		// start to level 0
		//$iLevel = 0;	
		for ($i=0; $i < count($_SESSION['aLinks']); $i++)
		{
			if ($_SESSION['aLinks'][$i]['bRoot'] == 1)
			{
				$this->oMenu->$i->iLnkID = $_SESSION['aLinks'][$i]['iLnkID'];
				$this->oMenu->$i->vLnkName = $_SESSION['aLinks'][$i]['vLnkName'];
				$this->oMenu->$i->vURL = $_SESSION['aLinks'][$i]['vURL'];
	
				$this->buildChild($this->oMenu->$i->childs, $_SESSION['aLinks'][$i]['iLnkID']);
			}
		}
	}
	
	// building the childs...
	private function buildChild(&$aChilds, $iParent)
	{
		// filtra solos los hijos
		$rsChilds = $_SESSION['aRelLinks'];
		$rsParents = $_SESSION['aLinks'];
		
		//print_r($rsParents);

		for ($i=0; $i < count($rsChilds); $i++)
		{
			// only the parent's childs
			if ($rsChilds[$i]['iRelParentID'] == $iParent)
			{
				for ($x=0; $x < count($rsParents); $x++)
				{
					if ($rsParents[$x]['iLnkID'] == $rsChilds[$i]['iRelChildID'])
					{
						$aChilds->$i->iLnkID = $rsParents[$x]['iLnkID'];
						$aChilds->$i->vLnkName = $rsParents[$x]['vLnkName'];
						$aChilds->$i->vURL = $rsParents[$x]['vURL'];

						$this->buildChild($aChilds->$i->childs, $rsParents[$x]['iLnkID']);
					}
				}
			}
		}
	}
	
	public function getMenu()
	{
		$this->buildMenu();
		//print_r($this->oMenu);
		return $this->oMenu;
	}
	
	// social menu
	private function buildSocialMenu($id)
	{
		global $oADODB;
		
		// get all the links
		$oLinks = new daoLinks($oADODB);
		// social filter
		$oLinks->addFilter("iSocialID", "=", $id);
		//$oLinks->addFilter("bRoot", "=", 1);
		$oLinks->addOrder(null, 'iLnkID', 'ASC');
		$rsLinks = $oLinks->getAll();
		// keep it in session		
		$_SESSION['aLinks'] = $rsLinks->getRows();
		
		for ($i=0; $i < count($_SESSION['aLinks']); $i++)
		{
			$this->oMenu->$i->iLnkID = $_SESSION['aLinks'][$i]['iLnkID'];
			$this->oMenu->$i->vLnkName = $_SESSION['aLinks'][$i]['vLnkName'];
			$this->oMenu->$i->vURL = $_SESSION['aLinks'][$i]['vURL'];
		}
	}	
	
	// return social menu
	public function getSocialMenu($id)
	{
		$this->buildSocialMenu($id);
		return $this->oMenu;
	}	
	
	// delete
	public function deleteMenu($id)
	{
		global $oADODB;
		
		// delete parent
		$oLinks = new daoLinks($oADODB);
		$oLinks->delete($id);
		
		// get childs
		$oRelLinks = new daoRelLinks($oADODB);
		$oRelLinks->addFilter("iRelParentID", "=", $id);
		$rsRelLinks = $oRelLinks->getAll();
		
		if (!$rsRelLinks->EOF)
		{
			$rsRelLinks = $rsRelLinks->getRows();
	
			for ($i=0; $i < count($rsRelLinks); $i++)
			{
				$oLinks->delete($rsRelLinks[$i]['iRelChildID']);
				
				$this->deleteChild($rsRelLinks[$i]['iRelChildID']);
			}
			
			$oRelLinks->deleteAll();
			
			// Enter first relation
			$oRelLinks->clean();
			$oRelLinks->addFilter("iRelChildID", "=", $id);
			$oRelLinks->deleteAll();	
		}
	}
	
	// delete childs
	private function deleteChild($id)
	{
		global $oADODB;
		$oLinks = new daoLinks($oADODB);
		
		// get childs
		$oRelLinks = new daoRelLinks($oADODB);
		$oRelLinks->addFilter("iRelParentID", "=", $id);
		$rsRelLinks = $oRelLinks->getAll();
		
		if (!$rsRelLinks->EOF)
		{
			$rsRelLinks = $rsRelLinks->getRows();
	
			for ($i=0; $i < count($rsRelLinks); $i++)
			{
				$oLinks->delete($rsRelLinks[$i]['iRelChildID']);
				
				$this->deleteChild($rsRelLinks[$i]['iRelChildID']);
			}
			
			$oRelLinks->deleteAll();
		}
	}
}
?>